package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.lance.answer.dto.Item;
import com.lance.answer.gui.answer.Question;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.time.Duration;
import java.util.*;

/**
 * <p>到处某门课程的所有题目<p>
 * <p>
 * <p>
 * <p>
 * <p>
 * 工程经济,size:1
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 14:47
 */
public class ExportLmsQuestionByTeacher {


    public static void main(String[] args) throws Exception {
        String driverPath = "d:\\ynouAutoStudy\\chromedriver.exe";
        ChromeDriver driver = Util.getDriver(driverPath);
        String url = "https://menhu.pt.ouchn.cn/site/ouchnPc/index";
        driver.get(url);
        Thread.sleep(3000);


        String userName = "19999206856";
        String pwd = "aa123456";
        String subjectName =
                "实用法律基础";
        WebElement userNameInput = driver.findElement(By.id("loginName"));
        userNameInput.clear();
        userNameInput.sendKeys(userName);
        //TimeUnit.SECONDS.sleep(1);

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.clear();
        passwordInput.sendKeys(pwd);
        //TimeUnit.SECONDS.sleep(1);

        // FIXME 要输入验证码
        Thread.sleep(5000);

        //webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginId"))).sendKeys(username);

        WebElement loginBtn = driver.findElement(By.name("button"));

        loginBtn.click();

        Thread.sleep(5000);

        driver.findElement(By.cssSelector("p[title='考试系统']")).click();

        Util.switchToLast(driver);

        Thread.sleep(5000);

        //     driver.get("https://ls.ks.ouchn.cn/#/app/examination/gradeManage/resultsSummary");


        driver.manage().window().maximize();
        Thread.sleep(1000 * 50);
        System.err.println("开始统计......");


        for (WebElement trWe : driver.findElements(By.cssSelector("tr[class='ant-table-row ant-table-row-level-0']"))) {

            System.err.println("trwe:" + trWe.getText());
            List<WebElement> tdList = trWe.findElements(By.tagName("td"));
            WebElement tdScore = tdList.get(11);
            System.err.println("tdScore:" + tdScore.getText());
            if ("~".equals(tdScore.getText())) {
                continue;
            }
//            trWe.click();
//
//            WebElement tdLast = tdList.get(tdList.size()-1);
//            tdLast.click();


//            for(WebElement btnWe:tdLast.findElement(By.tagName("div")).findElements(By.tagName("button"))){
//                if("详情".equals(btnWe.getText())){
//                  //  btnWe.click();
//                    System.err.println("---------------");
//                    Actions action = new Actions(driver);
//                    action.moveToElement(btnWe).click(btnWe).perform();
//                }
//            }

        }


        List<Question> questions = Lists.newArrayList();

        int twice = 10;
        while (twice>1){

            try {
                for (WebElement divWe : driver.findElements(By.cssSelector("div[class='question-wrapper']"))) {
                    //   System.err.println("divWe:"+divWe.getText());
                    Question question = new Question();
                    questions.add(question);

                    WebElement titleWe = divWe.findElement(By.cssSelector("div[class='question-title']"));
                    System.err.println("question-title:" + titleWe.getText());
                    question.setTitle(titleWe.getText());

                    List<Item> items = Lists.newArrayList();
                    question.setItems(items);
                    // question-content-wrapper
                    WebElement contentWe = divWe.findElement(By.cssSelector("div[class='question-content-wrapper']"));
                    for (WebElement we : contentWe.findElements(By.cssSelector("div[class='DraftEditor-editorContainer']"))) {
                        System.err.println("We:" + we.getText());
                        Item item = new Item();
                        item.setContent(we.getText());
                        items.add(item);
                    }
                    // answer-box
                    WebElement answerWe = divWe.findElement(By.cssSelector("div[class='answer-box']"));
                    System.err.println("answerWe:" + answerWe.getText());
                    String[] correctArray = new String[]{answerWe.getText()};
                    question.setCorrectArray(correctArray);
                }
            }catch (Exception e){

            }
            finally {
                twice--;
                System.err.println("end....");
                Thread.sleep(1000*20);
            }

        }


        Collections.sort(questions);
        List<Map<String, Object>> answers = new ArrayList<>();
        for (Question question : questions) {

            String title = question.getTitle();
            title = Util.dealContent(title);
            Map<String, Object> answerMap = new HashMap<>();
            answerMap.put("questionType", question.getQuestionType());
            answerMap.put("answerType", "click");

            answerMap.put("questionTypeDesc", question.getQuestionTypeDesc());
            answerMap.put("questionTitle", title);
            answerMap.put("questionCode", MD5Utils.encode(subjectName + title));
            answerMap.put("items", question.getItems());
            answerMap.put("answers", question.getCorrectArray());

            answers.add(answerMap);
        }

        FileUtils.write(new File("d://newAnswer2022//" + "合同法page4" + ".json"), JSONObject.toJSONString(answers, SerializerFeature.PrettyFormat), "utf-8");


        Thread.sleep(10000000);


//
    }


    private static List<WebElement> findWeElements(WebDriver driver, String cssSelector, int second) {
        return (new WebDriverWait(driver, Duration.ofSeconds(second))).until((ExpectedCondition<List<WebElement>>)
                d -> {
                    assert d != null;
                    return d.findElements(By.cssSelector(cssSelector));
                });
    }


}
